package com.scut.statistics_website.service;

import com.scut.statistics_website.endpoint.WebSocketEndpoint;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

@Component
public class MessageService {
    private static final Logger sysLogger = Logger.getLogger("sysLog");
    public void sendMessage(String message){
        List<Session> userSessions = WebSocketEndpoint.getUserSessions();
        if(userSessions.size() > 0) {
            for(Session session : userSessions) {
                try {
                    sysLogger.info(message);
                    synchronized (session) {
                        session.getBasicRemote().sendText(message);
                    }
                }
                catch (IOException e){
                    sysLogger.info("error in session");
                }
            }
        }
    }
}
